package com.quran.viewcontrollers;


import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bappi.utils.AlertUtils;
import com.bappi.utils.Constants;
import com.bappi.utils.Utils;
import com.custom.tab.AbstractTabRootManager;
import com.custom.tab.AbstractViewController;
import com.markupartist.android.widget.ActionBar;
import com.tos.quran.R;

public class SelectTranslationController extends AbstractViewController {

	private ActionBar actionBar;

	private List<String[]> languages = new ArrayList<String[]>();
	private LanguageListAdapter languageListAdapter;
	private ListView listView;

	

	public SelectTranslationController(AbstractTabRootManager tabRootManager) {
		super(tabRootManager, R.layout.select_translation);
		try {
			actionBar = (ActionBar) findViewById(R.id.actionbar);
			actionBar.setTitle(R.string.select_translation);
			listView = (ListView) findViewById(R.id.list_view);
			new AsyncTask<String, String, String>() {
				private AlertUtils.SpinnerProgressDialog spinnerProgressDialog;


				@Override
				protected void onPreExecute() {
					try {
						spinnerProgressDialog = AlertUtils.getProgressDialog(
								getActivity(),
								getString(R.string.dialog_default_title),
								getString(R.string.please_wait));
						spinnerProgressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				@Override
				protected String doInBackground(String... arg0) {
					try {
						JSONArray ja = new JSONArray(new String(
								Utils.getAssetsFileData(getActivity(),
										Constants.JSON_FILE_FOLDER_NAME
												+ "trans.json")));
						
						JSONObject jo;
						String[] row = {Constants.LANG_CODE_NONE,Constants.LANG_TITLE_NONE};
						languages.add(row);
						for (int i = 0; i < ja.length(); i++) {
							jo = ja.getJSONObject(i);
							row = new String[2];
							row[0] = jo.getString("code");
							row[1] = jo.getString("title");
							languages.add(row);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
						languageListAdapter = new LanguageListAdapter();
						listView.setAdapter(languageListAdapter);
						spinnerProgressDialog.dismiss();
						listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

							public void onItemClick(AdapterView<?> av, View v,
									int position, long arg) {
								languageSelected(position);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void languageSelected(int index) {
		try {
			final String tableName = languages.get(index)[0];
			boolean isNull = getActivity().getDatabaseAccessor()==null;
			Log.i("DREG", "BEFORE="+tableName+":isNull="+isNull);
			
			if ((Constants.LANG_CODE_NONE.equals(tableName))
					
					||(getActivity().getDatabaseAccessor().doesExistsTable(tableName))) {
				Log.i("DREG", "EDITING="+Constants.KEY_SELECTED_TRANS_CODE);
				getSharedPreferences().edit()
						.putString(Constants.KEY_SELECTED_TRANS_CODE,
								tableName).commit();
				
//				Intent intent = new Intent(getActivity(), SelectSura.class);
//				if (getActivity() instanceof Tab1) {
//					((Tab1) getActivity()).clearTop();
//				}
//				pushIntnet(intent);
			} else {
				Utils.showAlertDialog(getActivity(), "TABLE DOES NOT EXISTS, CHANGE MESSAGE");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class LanguageListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public LanguageListAdapter() {
			mInflater = (LayoutInflater) getActivity().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return languages.size();
		}

		public Object getItem(int arg0) {
			Object ret = null;
			if (languages != null && languages.size() > arg0) {
				ret = languages.get(arg0);
			}
			return ret;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.lan_list_content,
							null);
					holder = new ViewHolder();
					holder.mainContainer = convertView
							.findViewById(R.id.main_container);
					holder.title = (TextView) convertView
							.findViewById(R.id.title_view);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				holder.title.setText(languages.get(position)[1]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View mainContainer;
			TextView title;
		}
	}
}
